
//
//  NSString + Extension.swift
//  SinaMicroBlogSwift2
//
//  Created by dqw on 15/8/11.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

extension String {
    //        let string = "hahah<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>xixi"
    
    func sourceText() -> (link: String?, source:String?) {
        
        let pattern = "href=\"(.*?)\".*?>(.*?)</a>"
        let regu = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
        if  let ranges = regu.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
            let r1 = ranges.rangeAtIndex(1)
            let r2 = ranges.rangeAtIndex(2)
            let lk = (self as NSString).substringWithRange(r1)
            let sc = ( self as NSString).substringWithRange(r2)
            return (lk, sc)

        }
        return (nil, nil)
    }
}
